﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Stickman2D
{
	class Stickman : Sprite
	{
		const int START_POSITION_X = 125;
		const int START_POSITION_Y = 245;
		const int WIZARD_SPEED = 160;
		const int MOVE_UP = -1;
		const int MOVE_DOWN = 1;
		const int MOVE_LEFT = -1;
		const int MOVE_RIGHT = 1;
		const int SPRITE_WIDTH = 100;

		enum State
		{
			Walking,
			Jumping,
			Ducking
		}
		State mCurrentState = State.Walking;
		
		Vector2 mStartingPosition = Vector2.Zero;
		Vector2 mDirection = Vector2.Zero;
		Vector2 mSpeed = Vector2.Zero;

		KeyboardState mPreviousKeyboardState;
        
		public void LoadContent(ContentManager theContentManager)
		{
			Position = new Vector2(START_POSITION_X, START_POSITION_Y);
			
			List<string> sprites=new List<string>();
			sprites.Add("normal");
			sprites.Add("ducking");
			base.LoadContent(theContentManager, sprites);
			
			Source = new Rectangle(0, 0, SPRITE_WIDTH, Source.Height);
		}

		public void Update(GameTime theGameTime)
		{
			KeyboardState aCurrentKeyboardState = Keyboard.GetState();

			UpdateMovement(aCurrentKeyboardState);
			UpdateJump(aCurrentKeyboardState);
			UpdateDuck(aCurrentKeyboardState);

			mPreviousKeyboardState = aCurrentKeyboardState;

			base.Update(theGameTime, mSpeed, mDirection);
		}

		private void UpdateDuck(KeyboardState aCurrentKeyboardState)
		{
			if (aCurrentKeyboardState.IsKeyDown(Keys.RightShift) == true)
			{
				Duck();
			}
			else
			{
				StopDucking();
			}
		}

		private void Duck()
		{
			if (mCurrentState == State.Walking)
			{
				mSpeed = Vector2.Zero;
				mDirection = Vector2.Zero;

				CurrentTexture = "ducking";
				mCurrentState = State.Ducking;
			}
		}

		private void StopDucking()
		{
			if (mCurrentState == State.Ducking)
			{
				CurrentTexture = "normal";
				mCurrentState = State.Walking;
			}
		}

		private void UpdateJump(KeyboardState aCurrentKeyboardState)
		{
			if (mCurrentState == State.Walking)
			{
				if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true && mPreviousKeyboardState.IsKeyDown(Keys.Space) == false)
				{
					Jump();
				}
			}

			if (mCurrentState == State.Jumping)
			{
				if (mStartingPosition.Y - Position.Y > 150)
				{
					mDirection.Y = MOVE_DOWN;
				}

				if (Position.Y > mStartingPosition.Y)
				{
					Position.Y = mStartingPosition.Y;
					mCurrentState = State.Walking;
					mDirection = Vector2.Zero;
				}
			}
		}

		private void Jump()
		{
			if (mCurrentState != State.Jumping)
			{
				mCurrentState = State.Jumping;
				mStartingPosition = Position;
				mDirection.Y = MOVE_UP;
				mSpeed = new Vector2(WIZARD_SPEED, WIZARD_SPEED);
			}
		}

		private void UpdateMovement(KeyboardState aCurrentKeyboardState)
		{
			if (mCurrentState == State.Walking)
			{
				mSpeed = Vector2.Zero;
				mDirection = Vector2.Zero;

				if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
				{
					mSpeed.X = WIZARD_SPEED;
					mDirection.X = MOVE_LEFT;
				}
				else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
				{
					mSpeed.X = WIZARD_SPEED;
					mDirection.X = MOVE_RIGHT;
				}

				if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
				{
					mSpeed.Y = WIZARD_SPEED;
					mDirection.Y = MOVE_UP;
				}
				else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
				{
					mSpeed.Y = WIZARD_SPEED;
					mDirection.Y = MOVE_DOWN;
				}
			}
		}
	}
}
